package com.cz.pingfa;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

public class ProductionPlanSendTest {

    private static final String TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTA1NTI1NjQsInVzZXJuYW1lIjoiYWRtaW4ifQ.fKltpf6nzXxqF9bkaKOKukdZqD9W9MKY7Sbm1qyEzeQ";
    private static final String URL = "http://mes-service.sade.ant2world.com/production-plan/send";
    
    // 统计成功和失败的请求数
    private static final AtomicInteger successCount = new AtomicInteger(0);
    private static final AtomicInteger failureCount = new AtomicInteger(0);

    public static void main(String[] args) {

        Integer planId = 329;
        
        // 创建线程池，线程数可以根据需要调整
        int threadCount = 5;
        ExecutorService executor = Executors.newFixedThreadPool(threadCount);
        
        System.out.println("开始多线程发送生产计划，线程数: " + threadCount);
        System.out.println("待发送的计划ID: " + 329);
        
        // 为每个计划ID创建一个任务
        executor.submit(() -> {
            for (int i = 0; i < 10; i++) {
                sendProductionPlan(planId); 
            }
        });
        
        
        // 关闭线程池并等待所有任务完成
        executor.shutdown();
        try {
            if (executor.awaitTermination(60, TimeUnit.SECONDS)) {
                System.out.println("所有任务已完成");
            } else {
                System.out.println("部分任务超时，强制关闭线程池");
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            System.out.println("等待任务完成时被中断");
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }
        
        // 输出统计结果
        System.out.println("发送完成统计:");
        System.out.println("成功: " + successCount.get() + " 个");
        System.out.println("失败: " + failureCount.get() + " 个");
    }
    
    /**
     * 发送单个生产计划
     * @param planId 生产计划ID
     */
    private static void sendProductionPlan(Integer planId) {
        String threadName = Thread.currentThread().getName();
        System.out.println("线程 " + threadName + " 开始发送计划ID: " + planId);
        
        try {
            List<Integer> ids = Arrays.asList(planId);
            String param = JSONUtil.toJsonStr(ids);
            
            HttpResponse response = HttpRequest.post(URL)
                    .header("Authorization", "Bearer " + TOKEN)
                    .header("Content-Type", "application/json")
                    .body(param)
                    .timeout(30000) // 设置30秒超时
                    .execute();
            
            if (response.isOk()) {
                System.out.println("线程 " + threadName + " 成功发送计划ID: " + planId + ", 响应: " + response.body());
                successCount.incrementAndGet();
            } else {
                System.out.println("线程 " + threadName + " 发送计划ID: " + planId + " 失败, 状态码: " + response.getStatus() + ", 响应: " + response.body());
                failureCount.incrementAndGet();
            }
            
        } catch (Exception e) {
            System.out.println("线程 " + threadName + " 发送计划ID: " + planId + " 时发生异常: " + e.getMessage());
            failureCount.incrementAndGet();
        }
    }
}
